package project.Service;/*
 *  @Author Mr.long
 *  @QQ 1507468405
 *  @Creat 2021/12/8 - 9:40
 */

import org.json.JSONException;
import org.json.JSONObject;
import project.Dao.ContractDao;
import project.Dao.UserDao;
import project.Domain.Contract;
import project.userAED.userExportServlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.File;
import java.io.IOException;
import java.util.*;

@WebServlet(name = "ContractServlet", value = "/ContractServlet")
@MultipartConfig
public class ContractServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        service(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        service(request,response);
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        ContractDao dao = new ContractDao();
        UserDao userDao = new UserDao();
        JSONObject json = new JSONObject();
        try {
            //添加合同记录时的用户id
            String id = req.getParameter("user");
            int empID = Integer.parseInt(id);
            int leaderID = userDao.getLeaderID(empID);
            String boss = userDao.getNameById(leaderID);
            String mobileA = userDao.getMobileByID(leaderID);
            String employee = userDao.getNameById(empID);
            String mobileB = userDao.getMobileByID(empID);
            String title = req.getParameter("title");
            Part part = req.getPart("file");
            String time = req.getParameter("time");

            //构造contractFile指向文件路径
            String fileName = part.getSubmittedFileName();
            String realName = fileName.substring(0, fileName.lastIndexOf('.'));
            String suffix = fileName.substring(fileName.lastIndexOf('.'));
            String path = req.getSession().getServletContext().getRealPath("/") + "contractFile\\";
            String url = req.getContextPath() + "\\contractFile\\";
            String uuid = UUID.randomUUID().toString();
            String filename = realName + uuid + suffix;
            String realPath = url+filename;
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            }
            part.write(path + filename);

            Contract contract = new Contract(-1, boss, mobileA, employee, mobileB, title, realPath, time);
            if(dao.insert(contract)){
                json.put("result", 1);
                json.put("msg", "合同上传成功！！");
            }else{
                json.put("result", 1);
                json.put("msg", "合同上传成功！！");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        resp.setContentType("text/html; charset=UTF-8");
        resp.getWriter().print(json);
        resp.getWriter().flush();
        resp.getWriter().close();
    }
}
